package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.entity.PostText;
import com.zy.imagineCommunity.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PostManagementController {

    @Autowired
    PostService postService;

    @GetMapping("/postManagement")
    public String getPostManagement() {
        return "/PostManagement";
    }

    @ResponseBody
    @PostMapping("/toPostManagement")
    public JSONObject postManagement(int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<PostText> pageText = postService.queryPostByPageNoAndPageSize(page, 10);

        for (PostText postText : pageText.getItems()) {
            System.out.println(postText);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }

    /**
     * 删除帖子
     *
     * @param id
     */
    @ResponseBody
    @PostMapping("/deleteAdminPost")
    public void deleteAdminPost(int id) {
        //删除帖子
        postService.deletePostById(id);
        //删除帖子下面的聊天信息
        postService.deleteChatByPostId(id);
    }

    /**
     * 批量删除帖子
     *
     * @param idList
     */
    @ResponseBody
    @PostMapping("/deletePostList")
    public void deletePostList(String idList) {

        String[] id = idList.split(",");

        for (int i = 0; i < id.length; i++) {
            if (!"".equals(id[i])) {
                int idInt = Integer.parseInt(id[i]);
                //删除帖子
                postService.deletePostById(idInt);
                //删除帖子下面的聊天信息
                postService.deleteChatByPostId(idInt);
            }
        }

    }

}
